﻿using System;
using System.Linq;

namespace Centido.Management.ConsoleManager
{
    /// <summary>
    ///   A console command that lists all users from the current domain.
    /// </summary>
    internal class ListUsersCommand : Command
    {
        /// <summary>
        ///   The display all option parameter.
        /// </summary>
        public const string DisplayAllOptionParameter = "all";

        /// <summary>
        ///   The display all option parameter (short name).
        /// </summary>
        public const string DisplayAllOptionShortParameter = "i";


        /// <summary>
        ///   Gets the console command name.
        /// </summary>
        public override string Name
        {
            get { return "listusers"; }
        }


        /// <summary>
        ///   Gets the console command description.
        /// </summary>
        public override string Description
        {
            get { return Messages.ListUsersCommandDescription; }
        }

        
        /// <summary>
        ///   Gets the console command full description.
        /// </summary>
        public override string FullDescription
        {
            get { return Messages.ListUsersCommandFullDescription; }
        }


        /// <summary>
        ///   Executes the console command.
        /// </summary>
        /// <param name="context">The console command execution context.</param>
        /// <exception cref="ArgumentNullException">Thrown when the value of <paramref name="context"/> is null.</exception>
        public override void Execute(CommandExecutionContext context)
        {
            var domain = context.CurrentDomain;

            if (domain == null) return;

            // Enumerating all users
            foreach (var user in domain.Users.OrderBy(u => u.Name))
            {
                // If the display all option is set
                if (context.GetParameterByName(DisplayAllOptionParameter, DisplayAllOptionShortParameter) != null)
                {
                    // If the display name is set
                    if (!string.IsNullOrEmpty(user.DisplayName))
                        Console.WriteLine("{0} ({1})", user.Name, user.DisplayName);
                    else
                        Console.WriteLine(user.Name);
                }
                else
                    Console.WriteLine(user.Name);
            }

            Console.WriteLine();
        }
    }
}
